<?php
/**
 * Created by PhpStorm.
 * User: gyccvvh
 * Date: 2018-06-28
 * Time: 14:11
 */

namespace app\merchant\service;


use app\common\service\Service;
use think\Db;
use think\Log;

class Goods extends Service
{
    protected $goods_model = null;

    public function __construct(){
        parent::__construct();
        $this->goods_model = new \app\merchant\model\Goods();
    }

    public function getList($mer_id,$_where,$_form) {
        decorate_query($this->goods_model,$_where,$_form);
        $this->goods_model->where("mer_id",$mer_id);
        $records = $this->goods_model->paginate(15,false,[
            "query"=>$_form
        ]);
        if($records) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setData("goods",$records);
        }else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
                ->setMsg("获取商品列表失败");
        }
    }

    public function getTypes($mer_id, $_where, $_form) {
        $query = Db::name("GoodsType")->where("mer_id",$mer_id);
        decorate_query($query,$_where,$_form);
        $records = $this->goods_model->paginate(15,false,[
            "query" => $_form
        ]);
        if($records) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setData("types", $records);
        }else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Msg_Mer_Error)
                ->setMsg("获取商品类别失败");
        }
    }



    public function deleteType($mer_id, $type) {
        //检查是否有该类型的商品
        $res = $this->goods_model->getBy($mer_id,"type",$type);
        if($res) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
                ->setMsg(\app\merchant\define\Merchant::$Mer_Type_Has_Bind_Goods);
        }
        $res = Db::name("GoodsType")->where("id",$type)->delete();
        if($res){
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Success);
        }
        return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
            ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Error);
    }

    public function editType($_form,$id) {
        $res = Db::name("GoodsType")->where("id",$id)->update($_form);
        if($res){
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Edit_Success);
        }else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Edit_Error);
        }
    }

    public function addType($mer_id,$_form) {
        $_form['mer_id'] = $mer_id;
        $res = Db::name("GoodsType")->insert($_form);
        if($res) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Add_Success);
        }else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Add_Error);
        }
    }
    public function delete() {
        $id = input("id");
        Db::transaction();
        try{
            Db::name("GoodsSpec")->where("good_id",$id)->delete();
            Db::name("GoodsColor")->where("good_id",$id)->delete();
            Db::name("GoodsProps")->where("good_id",$id)->delete();
            $this->goods_model->where("id",$id)->delete();
            Db::commit();
            return $this->ret->setCode(0)->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Success)->setReload()->toJson();
        }catch(\Exception $e) {
            Db::rollback();
            Log::write($e->getMessage());
            return $this->ret->setCode(1)->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Error)->toJson();
        }
    }
    public function getOne($mer_id, $id) {
        $one = $this->goods_model->getById($mer_id,$id);
        if($one) {
            $one['goods_color'] = Db::name("GoodsColor")->where("goods_id",$one["id"])->select();
            $one["goods_spec"] = Db::name("GoodsSpec")->where("good_id",$one['id'])->select();
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setData("one",$one);
        }else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_No_Good);
        }
    }


    public function saveGoods($_form,$_spec_name, $_spec_price, $_color_name, $_color_img, $_color_price, $scene, $intended) {
        $check = $this->goods_model->checkForm($_form);
        if($check) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
                ->setMsg($check);
        }
        Db::startTrans();
        try{
            $goods_id = $this->goods_model->insertGetId($_form);
            $spec = [];
            foreach($_spec_name as $key => $_) {
                $spec[] = [
                    "good_id"   => $goods_id,
                    "name"      => $_,
                    "price"     => $_spec_price[$key]
                ];
            }
            Db::name("GoodsSpec")->insertAll($spec);
            $prop = select_object("company,season,code,material,style");
            $prop['scene'] = implode(",",$scene);
            $prop["intended"] = implode(",",$intended);
            $prop["good_id"] = $goods_id;
            Db::name("GoodsProps")->insert($prop);
            $colors = [];
            foreach ($_color_name as $key => $_) {
                $colors[] = [
                  "goods_id"    => $goods_id,
                  "color"       => $_,
                  "photo"       => $_color_img[$key],
                  "price"       => $_color_price[$key],
                ];
            }
            Db::name("GoodsColor")->insertAll($colors);
            Db::commit();
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Add_Success);
        }catch(\Exception $e) {
            Db::rollback();
            Log::write("DB Error:".$e->getTrace());
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Add_Error);
        }
    }

    public function updateDescript($id,$desc_url,$desc){
        $res = $this->goods_model->save([
            "description" => $desc,
            "description_url" => $desc_url
        ],["id"=>$id]);
        if($res){
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Success);
        }else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Error);
        }
    }

    public function updateGoods($_form,$_spec_name, $_spec_price, $_color_name, $_color_img, $_color_price, $scene, $intended) {
        Db::startTrans();
        try{
            $res = $this->goods_model->save($_form,["id"=>$_form['id']]);
            $prop = select_object("company,season,code,material,style");
            $prop['scene'] = implode(",",$scene);
            $prop["intended"] = implode(",",$intended);
            $res = Db::name("GoodsProps")->where("good_id",$_form['id'])->update($prop);

            Db::name("GoodsSpec")->where("good_id",$_form["id"])->delete();
            $spec = [];
            foreach($_spec_name as $key => $_) {
                $spec[] = [
                    "good_id"   => $_form['id'],
                    "name"      => $_,
                    "price"     => $_spec_price[$key]
                ];
            }
            Db::name("GoodsSpec")->insertAll($spec);
            Db::name("GoodsColor")->where("goods_id",$_form['id'])->delete();
            $colors = [];
            foreach ($_color_name as $key => $_) {
                $colors[] = [
                    "goods_id"    =>  $_form['id'],
                    "color"       => $_,
                    "photo"       => $_color_img[$key],
                    "price"       => $_color_price[$key],
                ];
            }
            Db::name("GoodsColor")->insertAll($colors);
            Db::commit();
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Edit_Success);
        }catch (\Exception $e) {
            Db::rollback();
            throw  $e;
//            Log::write("DB Error:".$e->getMessage());
//            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
//                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Edit_Error);
        }
    }

    public function putoff($id) {
        $res = $this->goods_model->save(['state' => 0],["id" => $id]);
        if($res) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Success);
        }else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Error);
        }
    }
}